--望国永恒
local wangguoyongheng = fk.CreateSkill {
    name = "mcdh_wangguoyongheng",
    tags = {Skill.Quest}
}

wangguoyongheng:addAcquireEffect(
    function(self,player, is_start)
        local room = player.room
        local events =  player.room.logic:getEventsOfScope(GameEvent.MoveCards,999, function(e)
            for _, move in ipairs(e.data) do
                if move.toArea == Card.DiscardPile then
                    return true
                end
            end
        end, Player.HistoryTurn)
        local suits = {}
        for _, e in ipairs(events) do
            for _, move in ipairs(e.data) do
                if move.toArea == Card.DiscardPile then
                    for _, info in ipairs(move.moveInfo) do
                        local suit = Fk:getCardById(info.cardId):getSuitString(true)
                        table.insertIfNeed(suits,suit)
                    end
                end
            end
        end
        if #suits ~= 0  then
            room:setPlayerMark(player,"@mcdh_wangguoyongheng-turn",suits)
        end

        local types = {}
        room.logic:getEventsOfScope(GameEvent.UseCard,1, function(e)
            local use = e.data[1]
            if use.extra_data and use.extra_data.mchd_zhaowendaoyi and use.from == player then
                table.insertIfNeed(types,use.card.type)
            end
        end, Player.HistoryGame)
        if #types ~= 0  then
            room:setPlayerMark(player,"@[cardtypes]mcdh_wangguoyongheng_use",types)
        end
    end
)

wangguoyongheng:addLoseEffect(function(self, player, isdead)
        local room = player.room
        room:setPlayerMark(player,"@mcdh_wangguoyongheng-turn",0)
        room:setPlayerMark(player,"@[cardtypes]mcdh_wangguoyongheng_use",0)
    end
)

wangguoyongheng:addEffect(fk.AfterCardsMove,{
    can_refresh = function(self, event, target, player, data)
            for _, move in ipairs(data) do
                if move.toArea == Card.DiscardPile then
                    return player:hasSkill(self,true)
                end
            end
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local  mark = player:getTableMark("@mcdh_wangguoyongheng-turn")
        for _, move in ipairs(data) do
            if move.toArea == Card.DiscardPile then
                for _, info in ipairs(move.moveInfo) do
                    local suit = Fk:getCardById(info.cardId):getSuitString(true)
                    table.insertIfNeed(mark,suit)
                end
            end
        end
        room:setPlayerMark(player,"@mcdh_wangguoyongheng-turn",mark)
    end,
})

wangguoyongheng:addEffect(fk.CardUseFinished,{
    can_refresh = function(self, event, target, player, data)
        return target == player and player:hasSkill(wangguoyongheng.name,true) and data.extra_data and data.extra_data.mchd_zhaowendaoyi
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local  mark = player:getTableMark("@[cardtypes]mcdh_wangguoyongheng_use")
        table.insertIfNeed(mark,data.card.type)
        room:setPlayerMark(player,"@[cardtypes]mcdh_wangguoyongheng_use",mark)
    end,
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(self) and not player:getQuestSkillState(wangguoyongheng.trueName)
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        if target == player then
            if data.extra_data and data.extra_data.mchd_zhaowendaoyi then
                return #player:getTableMark("@[cardtypes]mcdh_wangguoyongheng_use") == 3
            end
        end
        
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:updateQuestSkillState(player, wangguoyongheng.name,false)
        room:setPlayerMark(player,"mcdh_wangguoyongheng",1)

    end,
})

wangguoyongheng:addEffect(fk.TurnEnd,{
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(wangguoyongheng.name) and not player:getQuestSkillState(wangguoyongheng.name)
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        return #player:getTableMark("@mcdh_wangguoyongheng-turn") == 4
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        if not room.current.dead then
            room.current:drawCards(2,wangguoyongheng.name)
        end
    end,
})

wangguoyongheng:addEffect(fk.PindianCardsDisplayed,{
    is_delay_effect =true,
    can_trigger = function(self, event, target, player, data)
        return player:getMark("mcdh_wangguoyongheng") ~= 0 and (data.fromCard.color == Card.Black or #table.filter(data.results,function(result) return result.toCard.color == Card.Black  end)~= 0)
    end,
    on_cost=function ()
        return true
    end,
    on_use = function(self, event, target, player, data)
        player:drawCards(1,wangguoyongheng.name)
    end
})

Fk:loadTranslationTable {
    ["mcdh_wangguoyongheng"] = "望国永恒",
    [":mcdh_wangguoyongheng"] = "使命技，弃牌堆的牌置入过四种花色的回合结束时，你失去1点体力或减少1点体力上限。成功：当你使用三种类型的拼点牌后，本局中：当有角色亮出黑色拼点牌后，你摸一张牌。",
    ["#mcdh_wangguoyongheng_draw"] = "望国永恒",
    ["@mcdh_wangguoyongheng-turn"] = "望国永恒",
    ["@[cardtypes]mcdh_wangguoyongheng_use"] ="望国永恒",
}

return wangguoyongheng